package com.yan.furns.web.servlet;

import com.yan.furns.entity.Furn;
import com.yan.furns.entity.Page;
import com.yan.furns.service.FurnService;
import com.yan.furns.service.impl.FurnServiceImpl;
import com.yan.furns.utils.DataUtils;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author Yzb
 * @version 1.0
 */
public class FurnServlet extends BasicServlet {
	private FurnService furnService = new FurnServiceImpl();
	/**
	 * 使用模板设计模式+反射+动态绑定来调用到list方法
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		super.doGet(req, resp);
//		System.out.println("FurnServlet.list被调用");
		List<Furn> furns = furnService.queryFurns();
		req.setAttribute("furns", furns);
		req.getRequestDispatcher("/views/manage/furn_manage.jsp")
				.forward(req, resp);
	}



//	@Override
	protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		String name = req.getParameter("name");
//		String maker = req.getParameter("maker");
//		String price = req.getParameter("price");
//		String sales = req.getParameter("sales");
//		String stock = req.getParameter("stock");

		Furn furn = DataUtils.copyParameterToBean(req.getParameterMap(), new Furn());
//		Furn furn = new Furn();
//		try {
//			BeanUtils.populate(furn, req.getParameterMap());
//		} catch (Exception e) {
//			req.setAttribute("msg", "销量数据格式不正确...");
//			req.getRequestDispatcher("/views/manage/furn_add.jsp").forward(req, resp);
//			return;
//		}
		int res = furnService.insertFurn(furn);
		// res == 1 ? succeed : fail
//		System.out.println(res + "!!!");
//		System.out.println(req.getParameter("pageNo"));
		resp.sendRedirect(req.getContextPath() + "/manage/furnServlet?action=page&pageNo=" + req.getParameter("pageNo"));
	}

	protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		furnService.deleteFurn(DataUtils.parseInt(req.getParameter("id"), -1));
		resp.sendRedirect(req.getContextPath() + "/manage/furnServlet?action=page&pageNo=" + req.getParameter("pageNo"));
	}

	protected void showFurn(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Furn furn = furnService.queryFurn(DataUtils.parseInt(req.getParameter("id"), -1));
//		System.out.println("in showFurn() furn' id=" + furn.getId());
//		req.setAttribute("pageNo", req.getParameter("pageNo"));
		req.setAttribute("furn", furn);
		req.getRequestDispatcher("/views/manage/furn_update.jsp")
				.forward(req, resp);
	}

	protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Furn furn = DataUtils.copyParameterToBean(req.getParameterMap(), new Furn());
		int res = furnService.updateFurn(furn);
		resp.sendRedirect(req.getContextPath() + "/manage/furnServlet?action=page&pageNo=" + req.getParameter("pageNo"));
	}

	protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Integer pageSize = DataUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
		Integer pageNo = DataUtils.parseInt(req.getParameter("pageNo"), 1);

		Page<Furn> page = furnService.queryFurnPage(pageNo, pageSize);

		req.setAttribute("page", page);
		req.getRequestDispatcher("/views/manage/furn_manage.jsp")
				.forward(req, resp);
	}
}